package cate.game.role.bag.prop;

import cate.common.table.d.GDObj;
import cate.common.table.item.IBagItemTpl;
import cate.common.table.item.ItemBaseRow;
import cate.game.GameBody;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.bag.item.BaseBagItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Transient;

@Getter
@Setter
public class Prop extends BaseBagItem {
    @NoteField(value = "超时时间")
    private long expireTime;

    public Prop(){}

    public Prop(Role role, int tid, long num){
        super(role, GDObj.Type.PROP, tid, num);
        ItemBaseRow row = getTpl(role.toPlayBuildContext());
        if (row != null) {
            if (row.validTime > 0) {
                expireTime = System.currentTimeMillis() + 1000 * getTpl(role.toPlayBuildContext()).validTime;
            }
            if (row.expiredTime > 0) {
                expireTime = row.expiredTime;
            }
        }
    }

    @Override
    @JsonIgnore
    @Transient
    public ItemBaseRow getTpl(PlayBuildContext ctx) {
        return ctx.itemExcel().base.get(tid);
    }

    @Override
    public ItemBaseRow getTpl(GameBody game) {
        return getTpl(game.fight.getPlayBuildContext());
    }

    public boolean expired() {
        return expireTime > 0 && expireTime < System.currentTimeMillis();
    }

    /** 道具在一天内过期 */
    public boolean expiredInOneDay() {
        return expireTime > 0 && expireTime - System.currentTimeMillis() < 86400000;
    }
}
